<%-- 
    Document   : attemptMgmt
    Created on : Dec 18, 2011, 8:48:59 PM
    Author     : Randy
--%>

<%@page import="java.util.HashMap"%>
<%@page import="model.SystemUser"%>
<%@page import="model.Attempt"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Attempt Management</title>
        <%@include file = "global/admin_include.jsp" %>
        <%
            HashMap am = new HashMap();
            Object obj = request.getSession().getAttribute("accessMap");
            if (obj == null) {
                response.sendRedirect("../admin.jsp");
            } else {
                am = (HashMap) obj;
                if (am.get("A").equals("false")) {
                    response.sendRedirect("accessDenied.jsp");
                }
            }
        %>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#dd').dialog({modal:true});
                $('#dd').dialog('close');
                
                $('#tt').datagrid({
                    onBeforeEdit:function(index,row){  
                        row.editing = true;  
                        updateActions();  
                    },  
                    onAfterEdit:function(index,row){
                        row.editing = false;
                        $.ajax({
                            type: "POST",
                            url: "../AttemptServlet",
                            data: "action=updateAttempt"
                                + "&attempt_id=" + row.attempt_id
                                + "&status=" + row.status,
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                if((data.type!='error')){
                                    $('#tt').datagrid('reload');
                                }
                                alertMsg(data);
                            }
                        });
                        updateActions();
                    },  
                    onCancelEdit:function(index,row){
                        row.editing = false;  
                        updateActions(); 
                    }
                });
            });
            
            function formatActions(value,row,index){
                if (row.editing) {  
                    var s = '<input type="button" onclick="saverow('+index+')" value="Save"/> ';  
                    var c = '<input type="button" onclick="cancelrow('+index+')" value="Cancel"/>';  
                    return s+c;  
                } else {
                    var v = '<input type="button" onclick="attemptDetail('+row.attempt_id+')" value="Details"/>';
                    
                    if (row.status == "<%= Attempt.SurveyStatus.Completed%>" || row.status == "<%= Attempt.SurveyStatus.Acceptable%>") {
                        var e = '<input type="button" onclick="editrow('+index+')" value="Edit"/>';
                        var p = '<input type="button" onclick="getPDF('+row.attempt_id+')" value="PDF"/>';
                        
                        return p+e+v;
                    } else {
                        var d = '<input type="button" onclick="deleteAttempt('+row.attempt_id+')" value="Delete"/>';
                        
                        return d+v;
                    }
                }
            }
            
            function editrow(index){  
                $('#tt').datagrid('beginEdit', index);
            }  
             
            function saverow(index){  
                $('#tt').datagrid('endEdit', index);  
            }
            
            function cancelrow(index){  
                $('#tt').datagrid('cancelEdit', index);  
            }
            
            function updateActions(){  
                var rowcount = $('#tt').datagrid('getRows').length;  
                for(var i=0; i<rowcount; i++){ 
                    $('#tt').datagrid('updateRow',{index:i,row:{actions:''}});  
                }  
            }
            
            $(function() {
                $('#survey_status').change(function() {
                    $('#tt').datagrid({  
                        url:'../AttemptServlet?action=getAttempts&survey_status=' + $('#survey_status').val() + '&industry=<%=request.getParameter("industry")%>'
                    });
                });
            });
            
            function getPDF(attempt_id){
                window.open("../FrontEndServlet?action=getPDF&attempt_id="+attempt_id,"_blank");
            }
            
            function deleteAttempt(attempt_id){
                window.parent.$.messager.confirm("Confirm", "Delete attempt?", function(r){
                    if (r){
                        $.ajax({
                            type: "POST",
                            url: "../AttemptServlet",
                            data: "action=deleteAttempt"
                                + "&attempt_id=" + attempt_id,
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                if((data.type!='error')){
                                    $('#tt').datagrid('reload');
                                }
                                alertMsg(data);
                            }
                        });
                    }
                });
            }
            
            function attemptDetail(attempt_id){
                $('#country').val("");
                $('#title').val("");
                $('#company').val("");
                $('#company_size').val("");
                $('#it_dept_size').val("");
                $('#name').val("");
                $('#email').val("");
                
                $('#ind_tt').datagrid({
                    url:'../AttemptServlet?action=getAttemptThemeScore&attempt_id=' + attempt_id
                });

                $('#firm_tt').datagrid({
                    url:'../AttemptServlet?action=getAttemptThemeScore&attempt_id=' + attempt_id
                });
                        
                $('#question_tt').datagrid({
                    url:'../AttemptServlet?action=getAttemptScoring&attempt_id=' + attempt_id
                });
                        
                $.ajax({
                    type: "POST",
                    url: "../AttemptServlet",
                    data: "action=attemptDetail"
                        + "&attempt_id=" + attempt_id,
                    dataType: "json",
                    cache: false,
                    success: function(data){
                        
                        $('#country').val(data.country);
                        $('#title').val(data.title);
                        $('#company').val(data.company_name);
                        $('#company_size').val(data.company_size);
                        $('#it_dept_size').val(data.it_dept_size);


                        $.ajax({
                            type: "POST",
                            url: "../PublicUserServlet",
                            data: "action=getPublicUser"
                                + "&pu_id=" + data.pu_id,
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                $('#name').val(data.name);
                                $('#email').val(data.email);
                            }
                        });

                        
                    }
                });
                $('#dd').dialog('open');
            }
            
            function formatThemeFromThemeScore(value,row,index){ 
                return row.theme.theme_id;
            }
            
            function formatIndQuestion(value,row,index){ 
                return row.theme.ind_qns;
            }
            
            function formatFirmQuestion(value,row,index){ 
                return row.theme.firm_qns;
            }
            
            function formatQuestion(value,row,index){ 
                return row.question.question;
            }
            
            function formatQuestionID(value,row,index){ 
                return row.question.qns_id;
            }
        </script>
    </head>
    <body>
        <div style="border-bottom:1px solid #ccc"><h2>Attempt Management</h2></div> 
        <br/>
        <div id="tb" style="padding:5px;height:auto">
            <table width="100%">
                <tr>
                    <td width="80%">Status: 
                        <select name="survey_status" id="survey_status">
                            <option value=""></option>
                            <option value="<%= Attempt.SurveyStatus.Partial%>"><%= Attempt.SurveyStatus.Partial%></option>
                            <option value="<%= Attempt.SurveyStatus.Completed%>"><%= Attempt.SurveyStatus.Completed%></option>
                            <option value="<%= Attempt.SurveyStatus.Acceptable%>"><%= Attempt.SurveyStatus.Acceptable%></option>
                        </select>
                    </td>
                    <td width="20%" align="right">
                        <form action="../AttemptServlet?action=generateCSV&industry=<%=request.getParameter("industry")%>" method="post">
                            <input type="Submit" value="Generate CSV File"/>
                        </form>
                    </td>
                </tr>
            </table>
        </div>
        <div style="height:450px">
            <table id="tt" class="easyui-datagrid" fit="true"
                   toolbar="#tb"
                   singleSelect="true" fitColumns="true" pagination="false"
                   rownumbers="false">
                <thead>  
                    <tr>
                        <th field="attempt_id" width="5%">ID</th>
                        <th field="timestamps" width="15%">Date</th>
                        <th field="company_name" width="15%">Company</th>
                        <th field="title" width="10%">Title</th>
                        <th field="company_size" width="10%">Company Size</th>
                        <th field="it_dept_size" width="10%">IT Dept Size</th>
                        <th field="ack" width="10%">Report Viewed</th>
                        <th field="status" editor="{
                            type:'checkbox',
                            options:{on: '<%=Attempt.SurveyStatus.Acceptable.name()%>',off: '<%=Attempt.SurveyStatus.Completed.name()%>'}
                            }" width="10%" align="center">Status</th>
                        <th field="actions" width="15%" align="center" formatter="formatActions"></th>
                    </tr>  
                </thead>  
            </table> 
        </div>
        <div id="dd" title="Attempt Detail" style="width:955px; height:550px">
            <table cellspacing="10">
                <tr>
                    <td>Name:</td>
                    <td><input type="text" id="name" name="name" readonly="readonly"/></td>
                    <td>Email:</td>
                    <td><input type="text" id="email" name="email" readonly="readonly"/></td>
                    <td>Country:</td>
                    <td colspan="3"><input type="text" id="country" name="country" readonly="readonly"/></td>
                </tr>
                <tr>
                    <td>Company:</td>
                    <td><input type="text" id="company" name="company" readonly="readonly"/></td>
                    <td>Title:</td>
                    <td><input type="text" id="title" name="title" readonly="readonly"/></td>
                    <td>Company Size:</td>
                    <td><input type="text" id="company_size" name="company_size" readonly="readonly"/></td>
                    <td>IT Dept Size:</td>
                    <td><input type="text" id="it_dept_size" name="it_dept_size" readonly="readonly"/></td>
                </tr>
                <tr>
                    <td colspan="8">
                        <table id="ind_tt" class="easyui-datagrid"
                               singleSelect="true" fitColumns="true" pagination="false"
                               rownumbers="false" style="width:900px; height:235px">
                            <thead>  
                                <tr>
                                    <th field="theme_id" width="10%" formatter="formatThemeFromThemeScore" align="center">Theme</th>
                                    <th field="ind_qns" width="80%" formatter="formatIndQuestion">Industry Question</th>
                                    <th field="ind_score" width="10%" align="center">Score</th>
                                </tr>
                            </thead>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="8">
                        <table id="firm_tt" class="easyui-datagrid"
                               singleSelect="true" fitColumns="true" pagination="false"
                               rownumbers="false" style="width:900px; height:235px">
                            <thead>  
                                <tr>
                                    <th field="theme_id" width="10%" formatter="formatThemeFromThemeScore" align="center">Theme</th>
                                    <th field="firm_qns" width="80%" formatter="formatFirmQuestion">Firm Question</th>
                                    <th field="firm_score" width="10%" align="center">Score</th>
                                </tr>
                            </thead>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="8">
                        <table id="question_tt" class="easyui-datagrid"
                               singleSelect="true" fitColumns="true" pagination="false"
                               rownumbers="false" style="width:900px; height:235px">
                            <thead>  
                                <tr>
                                    <th field="theme_id" width="10%" align="center">Theme</th>
                                    <th field="qns_id" width="10%" formatter="formatQuestionID" align="center">Question ID</th>
                                    <th field="question" width="70%" formatter="formatQuestion">Question</th>
                                    <th field="score" width="10%" align="center">Score</th>
                                </tr>
                            </thead>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>

